define(window.backboneCommon.getViewDefine("campaign.detail"), function ($,require,Collection,tpl) {
    var backboneView= Backbone.View.extend({
        events: {
            "click #btn_submitSave": "submitSave"
        },
        initialize: function () {
            this.modelAttach={};
            this.modelAttach.customer=this.model.get("modelCustomer");
            this.modelAttach.baseData=this.model.get("modelBaseData");
            this.listenTo(this.modelAttach.customer, 'bind:AllList', this.bindCustomer);
            this.listenTo(this.modelAttach.baseData, "bind:DeliveryTimeType", this.bindDeliveryTimeType);
            this.listenTo(this.modelAttach.baseData, "bind:ResourceType", this.bindResourceType);
            this.listenTo(this.model, 'bind:Info', this.bindInfo);
            this.listenTo(this.model, 'save:Info', this.saveInfoCallback);
            this.render();
        },
        render:function(){
            var templateHtml=_.template(tpl);
            this.$el.html(templateHtml());
            return this;
        },
        initMain:function(id){
            this.elementModel={};
            this.extObject={};
            this.pageData={campaignId:id};
            $.ExtAssist.navBread.nav([{name:"投放管理"},{name:"订单管理",href:"#campaign"},{name:"订单详情"}]);
            this._initPagePower();
        },
        _initPagePower:function(){
            var self=this;
            $.ExtAssist.rolePower.operatePage("select",["advertiserList","edit","create"],function(json){
                self.pageData.power={
                    isOnlyView:json["select"],
                    isAdvertiser:json["advertiserList"],
                    isEdit:json["edit"],
                    isCreate:json["create"]
                };
                if(!self.pageData.power.isCreate && $.ExtCommon.isCheckEmpty(self.pageData.campaignId)){
                    $.ExtAssist.goUrlHref("#campaign",true);
                }else{
                    if(self.pageData.power.isEdit){self.pageData.power.isOnlyView=false;}
                    self._initBodyMain();
                }
            });
        },
        _initBodyMain:function(){
            this.elementModel.form=$("#form_detailCampaign");
            this.elementModel.resourceType=$("#panel_resourceType");
            this.collection.getInfo(this.pageData.campaignId);
            $.ExtAssist.urlParam.setUrl();
        },
        bindInfo:function(){
            var self=this;
            var result=this.model.get("dataInfo");
            self.pageData.isAdd=result.isAdd;
            var operateName="";
            if(self.pageData.isAdd){operateName="新建"}
            else{operateName=self.pageData.power.isOnlyView?"查看":"编辑"}
            $.ExtAssist.navBread.title($.ExtCommon.getStringConnect("{0}订单",operateName));
            self.extObject.formValue=self.elementModel.form.ExtFormValue({initValue:result.res.data,
                initCallback:function(dataJson){
                    self.pageData.campaignInfo=dataJson;
                    self.collection.initDetailAfter();
                    if(self.pageData.power.isAdvertiser){
                        self.collection.getCustomer();
                        if(self.pageData.isAdd){self.elementModel.resourceType.parents(".form-group:first").hide();}
                        else{self._getResourceTypeData(dataJson.customer);}
                    }else{
                        self._getResourceTypeData();
                    }
                    if(self.pageData.power.isOnlyView){
                        self.elementModel.form.find(":text,textarea").prop("disabled", true);
                        $("#btnGoReturn").text("返回");
                    }
                    self._initDateTime(dataJson);
                    $.ExtNumeric.initAll();
                    self.elementModel.form.ExtParsley();
                }
            });
            if (!result.res.success) {$.ExtNotify.error("该订单数据获取失败：" +result.res.msg)}
        },
        bindCustomer:function () {
            var self=this;
            var res=self.modelAttach.customer.get("dataAllList");
            var list=$.ExtCommon.convertListByPara(res.data,{name:"nameExt"});
            self.extObject.customer=$("#ddl_customer").ExtSelect2({placeholder: "-请选择-",allowClear: true,minimumResultsForSearch: 1, infoExt:{data:list,
                initCallback: function () {
                    if(!self.pageData.isAdd) {self.extObject.customer.disabled()}
                    if(self.pageData.power.isOnlyView){self.extObject.customer.disabled()}
                },
                changeCallback:function(value){
                    self._getResourceTypeData(value);
                }
            }});
        },
        //时间
        bindDeliveryTimeType:function () {
            var self=this;
            var res=self.modelAttach.baseData.get("dataDeliveryTimeType");
            this.extObject.deliveryTimeType=$("#panel_deliveryTimeType").ExtRadioCheckbox({type: "radio",attrName:"timeType",data:res.data,
                initCallback:function () {
                    self._changeDeliveryTimeType();
                    if(self.pageData.power.isOnlyView){self.extObject.deliveryTimeType.disabledAll()}
                },
                changeCallback:function () {
                    self._changeDeliveryTimeType();
                }
            });
        },
        _changeDeliveryTimeType:function(){
            var info=this.extObject.deliveryTimeType.getValueInfo();
            $("#panel_dayTime").toggle(info.isLong);
            $("#panel_rangeTime").toggle(info.isRange)
        },
        _initDateTime:function (json) {
            var self=this;
            var startLongTime,startTime,endTime;
            startLongTime=startTime=$.ExtMoment.initFormat();
            endTime=$.ExtMoment.initAdd(1,'y');
            if(json.isLong){
                startLongTime=json.startTime;
            }else if(json.isRange){
                startTime=json.startTime;
                endTime=json.endTime;
            }
            self.extObject.time=$("#txt_day").ExtDatePicker({initValue:startLongTime});
            self.extObject.timeRang=$("#txt_range").ExtDatePicker({type:"range",startValue:startTime,endValue:endTime,direction:"left"});
        },

        //资源类型
        _getResourceTypeData:function (customerId) {
            this.collection.getResourceType(customerId);
        },
        bindResourceType:function () {
            var self=this;
            var res=self.modelAttach.baseData.get("dataResourceType");
            if(self.extObject.resourceType){
                self.extObject.resourceType.replaceData(res.data);
            }else{
                self.extObject.resourceType = self.elementModel.resourceType.ExtRadioCheckbox({type:"radio", theme: "image", attrName: "resourceType", data: res.data,
                    initCallback: function () {
                        if(!self.pageData.isAdd) {self.extObject.resourceType.disabledAll()}
                        if(self.pageData.power.isOnlyView){self.extObject.resourceType.disabledAll()}
                    }
                });
            }
            self.elementModel.resourceType.parents(".form-group:first").show();
        },

        _checkForm:function(json){
            $.ExtNotify.destroy();
            var isExtCheck=true;
            if(!$.ExtCommon.isCheckEmpty(json.customer) && $.ExtCommon.isCheckEmpty(json.resourceType)){
                isExtCheck=false;
                $.ExtNotify.error("请选择资源类型");
            }
            return this.elementModel.form.ExtParsley("validate") && isExtCheck;
        },
        submitSave:function () {
            var ajaxData = this.elementModel.form.ExtFormValue("getValues");
            ajaxData.resourceType=this.extObject.resourceType.getValue();
            if(this._checkForm(ajaxData)) {
                var timeTypeInfo=this.extObject.deliveryTimeType.getValueInfo();
                if(timeTypeInfo.isLong){
                    ajaxData.startTime=this.extObject.time.getValue();
                }else if(timeTypeInfo.isRange){
                    var timeRangeValue=this.extObject.timeRang.getValue();
                    ajaxData.startTime=timeRangeValue[0];
                    ajaxData.endTime=timeRangeValue[1];
                }
                var infoOriginal = this.elementModel.form.ExtFormValue("getOriginalData");
                if (infoOriginal.id) {ajaxData.id = infoOriginal.id}
                this.collection.saveInfo(ajaxData);
            }
        },
        saveInfoCallback: function () {
            var result=this.model.get("dataSave");
            if (result.res.success) {
                $.ExtNotify.success("保存成功!");
                if(result.isAdd){$.ExtAssist.pageListParam.setData("campaignList",null)}
                $.ExtAssist.goUrlHref("#campaign");
            }else{
                $.ExtNotify.error(result.res.msg);
            }
        }
    });
    var viewModel=function(){
        this.collection= new Collection();
        this.model=this.collection.model;
        this.view=new backboneView({el: "#container_bodyMain",collection:this.collection,model:this.model});
    };
    viewModel.prototype={
        initView:function (param) {
            var id=param?param[0]:null;
            this.view.initMain(id);
        }
    };
    return viewModel;
});